package com.service;

import com.dao.DishTypeMapper;
import com.pojo.Dish;
import com.pojo.DishType;
import com.pojo.DishTypeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishTypeService {
    @Autowired
    private DishTypeMapper dishTypeMapper;

    public List<DishType> getDishType() {
        return dishTypeMapper.selectByExample(null);
    }
    //多了这行注释以及下面所有CRUD的方法，self
    //增加食品类型
    public void saveDishType(DishType dishType){
        dishTypeMapper.insertSelective(dishType);
    }

    //删除食品类型
    public void delectDishType(Integer dtId){
        dishTypeMapper.deleteByPrimaryKey(dtId);
    }

    //修改食品类型方法
    public void updateDishType(DishType dishType){
        //只是更新新的model中不为空的字段
        dishTypeMapper.updateByPrimaryKeySelective(dishType);
    }

    //根据食品类型的名称dtname进行模糊查询
    public List<DishType> getDishTypeByName(String dtName){
        //根据食品类别自定义生成查询类：DishTypeExample
        DishTypeExample dishTypeExample = new DishTypeExample();
        DishTypeExample.Criteria criteria = dishTypeExample.createCriteria();
        //进行模糊匹配，去除dtName两边的空格
        criteria.andDtNameLike("%" + dtName.trim() + "%");
        List<DishType> dishTypeList = dishTypeMapper.selectByExample(dishTypeExample);
        if (dishTypeList == null)
            return null;
        else
            return dishTypeList;
    }

    //验证"菜品名称"是否重复,相等-拒绝插入，不相等-插入数据
    public boolean checkDishTypebyName(String dtName){
        DishTypeExample dishTypeExample = new DishTypeExample();
        DishTypeExample.Criteria criteria = dishTypeExample.createCriteria();
        //看是否相等
        criteria.andDtNameEqualTo(dtName);
        long count = dishTypeMapper.countByExample(dishTypeExample);
        return count == 0;
    }
}
